<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: samples/cpp/segment_objects.cpp</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">samples/cpp/segment_objects.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<p>An example using drawContours to clean up a background segmentation result</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d1/d4f/imgproc_2include_2opencv2_2imgproc_8hpp.html">opencv2/imgproc.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../dc/d3d/videoio_8hpp.html">opencv2/videoio.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../db/d73/background__segm_8hpp.html">opencv2/video/background_segm.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;string&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d8/dcc/namespacestd.html">std</a>;</div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> help(<span class="keywordtype">char</span>** argv)</div><div class="line">{</div><div class="line">    printf(<span class="stringliteral">&quot;\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;This program demonstrated a simple method of connected components clean up of background subtraction\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;When the program starts, it begins learning the background.\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;You can toggle background learning on and off by hitting the space bar.\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;Call\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;%s [video file, else it reads camera 0]\n\n&quot;</span>, argv[0]);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> refineSegments(<span class="keyword">const</span> <a name="_a0"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; img, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; <a name="a1"></a><a class="code" href="../../da/dd3/group__gapi__math.html#gaba076d51941328cb7ca9348b7b535220">mask</a>, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; dst)</div><div class="line">{</div><div class="line">    <span class="keywordtype">int</span> niters = 3;</div><div class="line"></div><div class="line">    vector&lt;vector&lt;Point&gt; &gt; contours;</div><div class="line">    vector&lt;Vec4i&gt; hierarchy;</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> temp;</div><div class="line"></div><div class="line">    <a name="a2"></a><a class="code" href="../../d4/d86/group__imgproc__filter.html#ga4ff0f3318642c4f469d0e11f242f3b6c">dilate</a>(mask, temp, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>(), <a name="a3"></a><a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(-1,-1), niters);</div><div class="line">    <a name="a4"></a><a class="code" href="../../d4/d86/group__imgproc__filter.html#gaeb1e0c1033e3f6b891a25d0511362aeb">erode</a>(temp, temp, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>(), <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(-1,-1), niters*2);</div><div class="line">    <a class="code" href="../../d4/d86/group__imgproc__filter.html#ga4ff0f3318642c4f469d0e11f242f3b6c">dilate</a>(temp, temp, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>(), <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(-1,-1), niters);</div><div class="line"></div><div class="line">    <a name="a5"></a><a class="code" href="../../d3/dc0/group__imgproc__shape.html#gadf1ad6a0b82947fa1fe3c3d497f260e0">findContours</a>( temp, contours, hierarchy, <a name="a6"></a><a class="code" href="../../d3/dc0/group__imgproc__shape.html#gga819779b9857cc2f8601e6526a3a5bc71a7d1d4b509fb2a9a8dc2f960357748752">RETR_CCOMP</a>, <a name="a7"></a><a class="code" href="../../d3/dc0/group__imgproc__shape.html#gga4303f45752694956374734a03c54d5ffa5f2883048e654999209f88ba04c302f5">CHAIN_APPROX_SIMPLE</a> );</div><div class="line"></div><div class="line">    dst = Mat::zeros(img.<a name="a8"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#a146f8e8dda07d1365a575ab83d9828d1">size</a>(), <a name="a9"></a><a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga88c4cd9de76f678f33928ef1e3f96047">CV_8UC3</a>);</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span>( contours.size() == 0 )</div><div class="line">        <span class="keywordflow">return</span>;</div><div class="line"></div><div class="line">    <span class="comment">// iterate through all the top-level contours,</span></div><div class="line">    <span class="comment">// draw each connected component with its own random color</span></div><div class="line">    <span class="keywordtype">int</span> idx = 0, largestComp = 0;</div><div class="line">    <span class="keywordtype">double</span> maxArea = 0;</div><div class="line"></div><div class="line">    <span class="keywordflow">for</span>( ; idx &gt;= 0; idx = hierarchy[idx][0] )</div><div class="line">    {</div><div class="line">        <span class="keyword">const</span> vector&lt;Point&gt;&amp; c = contours[idx];</div><div class="line">        <span class="keywordtype">double</span> area = fabs(<a name="a10"></a><a class="code" href="../../d3/dc0/group__imgproc__shape.html#ga2c759ed9f497d4a618048a2f56dc97f1">contourArea</a>(<a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>(c)));</div><div class="line">        <span class="keywordflow">if</span>( area &gt; maxArea )</div><div class="line">        {</div><div class="line">            maxArea = area;</div><div class="line">            largestComp = idx;</div><div class="line">        }</div><div class="line">    }</div><div class="line">    <a name="_a11"></a><a class="code" href="../../d1/da0/classcv_1_1Scalar__.html">Scalar</a> color( 0, 0, 255 );</div><div class="line">    <a name="a12"></a><a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga746c0625f1781f1ffc9056259103edbc">drawContours</a>( dst, contours, largestComp, color, <a name="a13"></a><a class="code" href="../../d6/d6e/group__imgproc__draw.html#ggaf076ef45de481ac96e0ab3dc2c29a777a89c5f6beef080e6df347167f85e07b9e">FILLED</a>, <a name="a14"></a><a class="code" href="../../d6/d6e/group__imgproc__draw.html#ggaf076ef45de481ac96e0ab3dc2c29a777a5d32eda7017db273a37f158e5b51442a">LINE_8</a>, hierarchy );</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv)</div><div class="line">{</div><div class="line">    <a name="_a15"></a><a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html">VideoCapture</a> cap;</div><div class="line">    <span class="keywordtype">bool</span> update_bg_model = <span class="keyword">true</span>;</div><div class="line"></div><div class="line">    <a name="_a16"></a><a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html">CommandLineParser</a> parser(argc, argv, <span class="stringliteral">&quot;{help h||}{@input||}&quot;</span>);</div><div class="line">    <span class="keywordflow">if</span> (parser.<a name="a17"></a><a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html#a8c41cf242b2ad8c0472e3b57893ef200">has</a>(<span class="stringliteral">&quot;help&quot;</span>))</div><div class="line">    {</div><div class="line">        help(argv);</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line">    <span class="keywordtype">string</span> input = parser.<a name="a18"></a><a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html#a190215863579a6d244dccfd261ee0224">get</a>&lt;std::string&gt;(<span class="stringliteral">&quot;@input&quot;</span>);</div><div class="line">    <span class="keywordflow">if</span> (input.empty())</div><div class="line">        cap.<a name="a19"></a><a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#a614a1702e15f42ede5100014ce7f48ed">open</a>(0);</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">        cap.<a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#a614a1702e15f42ede5100014ce7f48ed">open</a>(<a name="a20"></a><a class="code" href="../../d6/dba/group__core__utils__samples.html#ga4a37595bcd267558bd6c176c7307f050">samples::findFileOrKeep</a>(input));</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span>( !cap.<a name="a21"></a><a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#a9d2ca36789e7fcfe7a7be3b328038585">isOpened</a>() )</div><div class="line">    {</div><div class="line">        printf(<span class="stringliteral">&quot;\nCan not open camera or video file\n&quot;</span>);</div><div class="line">        <span class="keywordflow">return</span> -1;</div><div class="line">    }</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> tmp_frame, bgmask, out_frame;</div><div class="line"></div><div class="line">    cap &gt;&gt; tmp_frame;</div><div class="line">    <span class="keywordflow">if</span>(tmp_frame.empty())</div><div class="line">    {</div><div class="line">        printf(<span class="stringliteral">&quot;can not read data from the video source\n&quot;</span>);</div><div class="line">        <span class="keywordflow">return</span> -1;</div><div class="line">    }</div><div class="line"></div><div class="line">    <a name="a22"></a><a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>(<span class="stringliteral">&quot;video&quot;</span>, 1);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>(<span class="stringliteral">&quot;segmented&quot;</span>, 1);</div><div class="line"></div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga6395ca871a678020c4a31fadf7e8cc63">Ptr&lt;BackgroundSubtractorMOG2&gt;</a> bgsubtractor=<a name="a23"></a><a class="code" href="../../de/de1/group__video__motion.html#ga2beb2dee7a073809ccec60f145b6b29c">createBackgroundSubtractorMOG2</a>();</div><div class="line">    bgsubtractor-&gt;setVarThreshold(10);</div><div class="line"></div><div class="line">    <span class="keywordflow">for</span>(;;)</div><div class="line">    {</div><div class="line">        cap &gt;&gt; tmp_frame;</div><div class="line">        <span class="keywordflow">if</span>( tmp_frame.empty() )</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        bgsubtractor-&gt;apply(tmp_frame, bgmask, update_bg_model ? -1 : 0);</div><div class="line">        refineSegments(tmp_frame, bgmask, out_frame);</div><div class="line">        <a name="a24"></a><a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(<span class="stringliteral">&quot;video&quot;</span>, tmp_frame);</div><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(<span class="stringliteral">&quot;segmented&quot;</span>, out_frame);</div><div class="line">        <span class="keywordtype">char</span> keycode = (char)<a name="a25"></a><a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(30);</div><div class="line">        <span class="keywordflow">if</span>( keycode == 27 )</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        <span class="keywordflow">if</span>( keycode == <span class="charliteral">&#39; &#39;</span> )</div><div class="line">        {</div><div class="line">            update_bg_model = !update_bg_model;</div><div class="line">            printf(<span class="stringliteral">&quot;Learn background is in state = %d\n&quot;</span>,update_bg_model);</div><div class="line">        }</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:29 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
